//
//  ONSetUpHomeViewController.m
//  OneEightyNine
//
//  Created by Mac on 2019/3/5.
//  Copyright © 2019 stoneobs. All rights reserved.
//

#import "ONSetUpHomeViewController.h"
#import "OEHowItWorksViewController.h"
#import "OEPrivacyStationViewController.h"
#import "OEPaymentSettingViewController.h"
#import "OEAboutUsViewController.h"
#import "OESuggestViewController.h"
@interface ONSetUpHomeViewController ()
@property(nonatomic, strong) NSArray                     *dataSouce;/**< <##> */
@end

@implementation ONSetUpHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configSubView];
   
    [self configDataSouce];
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gotoDebug)];
    tap.numberOfTapsRequired = 5;
    [self.view addGestureRecognizer:tap];
    // Do any additional setup after loading the view.
}
- (void)configDataSouce{
    NSUInteger intg = [[SDImageCache sharedImageCache] getSize];
    //
    NSString * currentVolum = [NSString stringWithFormat:@"%0.2fM",intg/1024/1024.0];
    self.dataSouce = @[
                       [[STTableEasyModel alloc] initWithTextString:OESTRING(@"HAO IT WORKS") detailString:@""],
                       [[STTableEasyModel alloc] initWithTextString:OESTRING(@"PRIVACY STATION") detailString:@""],
                       [[STTableEasyModel alloc] initWithTextString:OESTRING(@"PAYMENT SETTING") detailString:@""],
                       [[STTableEasyModel alloc] initWithTextString:OESTRING(@"SCAVENGING CACHING") detailString:currentVolum],
                       [[STTableEasyModel alloc] initWithTextString:OESTRING(@"ABOUNT US") detailString:@""],
                       [[STTableEasyModel alloc] initWithTextString:OESTRING(@"FEEDBACK") detailString:@""],
                       ];
    [self.tableView reloadData];
}
#pragma mark --subView
- (void)configSubView{
    UIView * header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 130)];
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(self.insetX, 0, UIScreenWidth, 130)
                                                     text:OESTRING(@"SET UP")
                                                textColor:UIColor.blackColor
                                                     font:18
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    [header addSubview:titleLable];
    self.tableView.tableHeaderView = header;
    
    __weak typeof(self) weakSelf =  self;
    UIView * footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 100)];
    footer.backgroundColor = self.tableView.backgroundColor;
    STButton * confimButton = [[STButton alloc] initWithFrame:CGRectMake(30, 0, UIScreenWidth - 60 , 44)
                                                        title:@"EXIT LOGON"
                                                   titleColor:[UIColor whiteColor]
                                                    titleFont:18
                                                 cornerRadius:22
                                              backgroundColor:OE_LIMITE_COLOR
                                              backgroundImage:nil
                                                        image:nil];
    [confimButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedLogoutButton];
    }];
    confimButton.st_centerY = 50;
    confimButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [footer addSubview:confimButton];
    self.tableView.tableFooterView = footer;
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSouce.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseIdentifier];
        
    }
    STTableEasyModel * model = self.dataSouce[indexPath.row];
    [cell setSt_tableEasyModel:model];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    STTableEasyModel * model = self.dataSouce[indexPath.row];
    if ([model.textString isEqualToString:@"SCAVENGING CACHING"]) {
        [self st_showAlertTitle:@"please confim to clear the cache？" message:@"" leftTitle:@"cancle" rightTitle:@"yes" block:^(NSString *name) {
            if ([name isEqualToString:@"yes"]) {
                [[SDImageCache sharedImageCache] clearDisk];
                [self configDataSouce];
            }
        }];
    }
    if ([model.textString isEqualToString:@"HAO IT WORKS"]) {
        [self.navigationController pushViewController:OEHowItWorksViewController.new animated:YES];
    }
    if ([model.textString isEqualToString:@"PRIVACY STATION"]) {
        [self.navigationController pushViewController:OEPrivacyStationViewController.new animated:YES];
    }
    if ([model.textString isEqualToString:@"PAYMENT SETTING"]) {
        [self.navigationController pushViewController:OEPaymentSettingViewController.new animated:YES];
    }
    if ([model.textString isEqualToString:@"SCAVENGING CACHING"]) {
        //        [self.navigationController pushViewController:OEPrivacyStationViewController.new animated:YES];
    }
    if ([model.textString isEqualToString:@"ABOUNT US"]) {
                [self.navigationController pushViewController:OEAboutUsViewController.new animated:YES];
    }
    if ([model.textString isEqualToString:@"FEEDBACK"]) {
                [self.navigationController pushViewController:OESuggestViewController.new animated:YES];
    }
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

#pragma mark --Action Method
- (void)onSelectedLogoutButton{
    [self st_showAlertWithTitle:@"please confim to login out?" titleColor:FirstTextColor message:@"" messageColor:nil leftTitle:@"cancle" leftTitleColor:FlatYellow rightTitle:@"yes" rightTitleColor:ThirdTextColor handle:^(NSString *name) {
        if ([name isEqualToString:@"yes"]) {
            [STUserManger.defult removeUserPreferce];
            UIWindow * window =  UIApplication.sharedApplication.delegate.window;
            STNavigationController * nav = [[STNavigationController alloc] initWithRootViewController:OELoginHomeViewController.new];
            window.rootViewController = nav;
            [window st_showAnimationWithType:STAnimationTypecube];
            
        }
    }];
}
- (void)gotoDebug{
    STNavigationController * nav = [[STNavigationController alloc] initWithRootViewController:STMonitorHomeViewController.new];
    [self presentViewController:nav animated:YES completion:^{
        
    }];
}
@end
